// @ts-ignore
import { sm4 as Sm4js } from 'gm-crypt';

export default class CryptoSm4js {
  private config = {
    key: 'r81CqvyF6pWZUeaj',
    mode: 'cbc',
    iv: 'i1XjdJM21mpNSLLV',
    cipherType: 'base64',
  };
  sm4js: any;
  constructor() {
    this.sm4js = new Sm4js(this.config);
  }

  toString(value: object | string): string {
    if (typeof value !== 'string') {
      return JSON.stringify(value);
    }
    return value;
  }

  encrypt(str: object | string): string {
    str = this.toString(str);
    return this.sm4js.encrypt(str);
  }

  decrypt(str: object | string): string {
    str = this.toString(str);
    return this.sm4js.decrypt(str);
  }
}
